package com.nsn.das.midas.service.assembler.impl;

import java.util.List;

import javax.annotation.Resource;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.stereotype.Component;

import com.nsn.das.midas.model.component.Build;
import com.nsn.das.midas.model.component.NetworkElement;
import com.nsn.das.midas.model.embedded.State;
import com.nsn.das.midas.model.embedded.Status;
import com.nsn.das.midas.model.embedded.Version;
import com.nsn.das.midas.model.mml.data.MMLClassData;
import com.nsn.das.midas.model.mml.letter.MMLClassLetter;
import com.nsn.das.midas.model.user.DASUser;
import com.nsn.das.midas.service.MMLClassManager;
import com.nsn.das.midas.service.assembler.CommandClassAssembler;
import com.nsn.das.utils.DateUtils;

@Component("commandClassAssembler")
public class CommandClassAssemblerImpl extends CommonAssemblerImpl implements CommandClassAssembler{
	@Resource(name = "mmlClassManager")
	private MMLClassManager mmlClassManager;
	
	@Override
	public MMLClassData retriveClassDataFromJson(JSONArray array) throws Exception{
		return retriveClassDataFromJson(array.getJSONObject(0)
				,baseManager.getEntity(DASUser.class,array.getLong(1)));
	}
	
	@Override
	public MMLClassData retriveClassDataFromJson(JSONObject data, DASUser editor) throws Exception{
		MMLClassData mcc;
		long id = Long.parseLong(data.getString("id"));
		if(id > 0){
			mcc = baseManager.getEntity(MMLClassData.class,id);
		}else{
			mcc = new MMLClassData();
		}
		mcc.setClassLetter(baseManager.getEntity(MMLClassLetter.class, data.getLong("classLetterId")));
		mcc.setState(new State(data.getString("state").toUpperCase()));
		mcc.setAcceptedDate(DateUtils.string2Date(data.getString("acceptedDate")));
		mcc.setGrammarChecked(data.getBoolean("grammar"));
		mcc.setMenuText(data.getString("menuText"));
		mcc.setNetworkElements(baseManager.getEntitysByIds(NetworkElement.class,data.getString("networkElements")));
		mcc.setApplication(data.getString("application"));
		mcc.setBuilds(baseManager.getEntitysByIds(Build.class,data.getString("builds")));
		mcc.setStatus(new Status(data.getString("status")));
		mcc.setAuthor(getUserFromJson(data.getJSONObject("author")));
		mcc.setCreatedAt(DateUtils.string2Date(data.getString("createdAt")));
		mcc.setVersion(new Version(data.getString("version")));
		mcc.getHistories().add(buildRevisionHistory(editor,data.getString("comments")));
		return mcc;
	}
	
	@Override
	public JSONObject buildClassDataJson(long id) throws Exception {
		MMLClassData model = baseManager.getEntity(MMLClassData.class,id);
		JSONObject json = new JSONObject();
		json.put("id", model.getId());
		json.put("classLetter", buildClassLetterJson(model.getClassLetter()));
		json.put("state",model.getState().getDescription().toUpperCase());
		json.put("acceptedDate", DateUtils.date2String(model.getAcceptedDate()));
		json.put("grammar", model.isGrammarChecked());
		json.put("menuText", model.getMenuText());
		json.put("builds", buildBuildsJson(model.getBuilds()));
		json.put("networkElements", buildNetworkElementJson(model.getNetworkElements()));
		json.put("application", model.getApplication());
		json.put("status", model.getStatus().getDescription());
		json.put("author", buildAuthorJson(model.getAuthor()));
		json.put("createdAt", DateUtils.date2String(model.getCreatedAt()));
		json.put("version", model.getVersion().getVersion());
		json.put("revisionHistory", buildRevisionHistory(model.getHistories()));
		return json;
	}
	
	private JSONObject buildClassLetterJson(MMLClassLetter classLetter) throws JSONException{
		JSONObject object = new JSONObject();
		object.put("id", classLetter.getId());
		object.put("letter", classLetter.getLetter());
		return object;
	}

	@Override
	public JSONArray buildClassDataListJson(int pageIndex, int pageSize) {
		JSONArray result = new JSONArray();
		List<MMLClassData> models = baseManager.getEntities(MMLClassData.class, pageIndex, pageSize);
		try {
			result.put(baseManager.countEntities(MMLClassData.class));
			for(MMLClassData model : models){
				JSONObject o = new JSONObject();
				o.put("id", model.getId());
				o.put("classLetter",model.getClassLetter().getLetter());
				o.put("menuText",model.getMenuText());
				o.put("version",model.getVersion().getVersion());
				o.put("state",model.getState().getDescription().toUpperCase());
				result.put(o);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public JSONArray buildAvailableLetterJson() {
		JSONArray array = new JSONArray();
		try {
			for(MMLClassLetter letter : mmlClassManager.getAvailableClassLetters()){
				array.put(buildClassLetterJson(letter));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return array;
	}
	
}
